package cs236369.proxy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.ParseException;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class ProxyServerTest {
	
	ProxyServer testServer = null;
	
	//@Before
	public void init() throws FileNotFoundException, IOException, ClassNotFoundException {
		
		Properties p = new Properties();
		p.load(new FileInputStream("config"));
		testServer = new ProxyServer(p);
		testServer.start();
	}
	
	//@Test
	public void recTest() throws IOException, HttpException {
		Socket socket = new Socket("132.68.238.21",80);
		// Initialize socket
		HttpParams params = new BasicHttpParams(); 
		DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
		conn.bind(socket, params);
		HttpRequest request = new BasicHttpRequest("GET", "/");
		conn.sendRequestHeader(request);
		System.err.println("Sent succ");
		HttpResponse response = conn.receiveResponseHeader();
		System.out.println(response.getStatusLine());
		conn.receiveResponseEntity(response);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
		    // Do something useful with the entity and, when done, ensure all 
		    // content has been consumed, so that the underlying connection 
		    // coult be re-used 
		    EntityUtils.consume(entity);
		}
	}
	
	/**
	 * Testing a simple HEADER sending
	 */
	//@Test
	public void testProxyServer() {
		
		try {
			
			Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 8085);
			
			// Initialize socket
			HttpParams params = new BasicHttpParams(); 
			DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, "UTF-8");
			params.setParameter("Host", InetAddress.getLocalHost().getHostAddress());
			
	        HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
	        
			conn.bind(socket, params);
			HttpRequest request = new BasicHttpRequest("GET", "/");
			Header header = new Header() {
				
				@Override
				public String getValue() {
					return "www.google.com";
				}
				
				@Override
				public String getName() {
					return "Host";
				}
				
				@Override
				public HeaderElement[] getElements() throws ParseException {
					return null;
				}
			};
			request.addHeader(header);
			conn.sendRequestHeader(request);
			System.out.println(conn.isOpen());
			conn.close();

		} catch (Exception e) {
			System.out.println("Test Error Trace:");
			e.printStackTrace();
		} 
	}
	
	/**
	 * Sending post request 
	 */
	/*@Test
	public void testPOST() {
		
		try {
			Socket s = new Socket("127.0.1.1", 8085);
			String req = "GET http://localhost:54321/x HTTP/1.1\r\n" +
					"Host: localhost:54321\r\n" +
					"Cache-Control: no-cache\r\n" +
					"\r\n";
			s.getOutputStream().write(req.getBytes());
			s.getOutputStream().flush();	
		} finally {
			try { s.close(); } catch (Exception e) {}
			t.join();
		}
	}*/
	
	@Test
	public void streamTest() {
		
		   StringTokenizer st = new StringTokenizer("this\r\nis\r\n a\r\ntest\r\n","\r\n",false);
		     while (st.hasMoreTokens()) {
		         System.out.println(st.nextToken());
		     }
		     System.out.println("end");
	}

}
